package com.wxiao.exam.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxiao.exam.MajorService;
import com.wxiao.exam.PageBean;
import com.wxiao.exam.Result;
import com.wxiao.exam.mapper.MajorMapper;
import com.wxiao.exam.pojo.MajorDO;
import com.wxiao.exam.pojo.vo.GradeVO;
import com.wxiao.exam.pojo.vo.MajorVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <br/>
 * Created by wangxiao on 2019/07/07
 */
@Service("majorService")
public class MajorServiceImpl implements MajorService {

    private final static Logger LOG = LoggerFactory.getLogger(MajorServiceImpl.class);

    @Resource
    private MajorMapper majorMapper;

    @Override
    public Result addMajor(MajorVO majorVO) throws Exception {
        if (findGradeByName(majorVO.getName())) {
            return Result.getFailure("专业已经存在");
        } else {
            majorMapper.addMajor(majorVO);
            return Result.getSuccess("添加成功");
        }
    }

    /**
     * 根据年级名称查询年级是否存在
     *
     * @param name 年级信息
     * @return true 存在， false 不存在
     */
    public boolean findGradeByName(String name) {
        MajorVO majorVO = new MajorVO();
        majorVO.setName(name);
        List<MajorDO> majorDOS = majorMapper.listMajor(majorVO);
        if (majorDOS != null && !majorDOS.isEmpty()) {
            return true;
        }
        return false;
    }


    @Override
    public Result updateMajor(MajorVO majorVO) throws Exception {
        return null;
    }

    @Override
    public Result findDetailMajor(MajorVO majorVO) throws Exception {
        return null;
    }

    @Override
    public PageBean<MajorDO> listMajor(MajorVO query) throws Exception {
        PageHelper.startPage(query.getPageCode(), query.getPageSize());
        List<MajorDO> majorDOS = majorMapper.listMajor(query);
        PageInfo<MajorDO> info = new PageInfo<>(majorDOS);
        PageBean<MajorDO> major = new PageBean<MajorDO>(majorDOS, info.getPageSize(),
                query.getPageCode(), (int) info.getTotal(), query.getSize());
        return major;
    }

    @Override
    public Result listMajorPage(MajorVO majorVO) throws Exception {
        return null;
    }

    @Override
    public Result countMajor(MajorVO majorVO) throws Exception {
        return null;
    }

    @Override
    public Result deleteMajor(MajorVO majorVO) throws Exception {
        return null;
    }

    @Override
    public List<MajorDO> listAll() {
        return majorMapper.listMajor(new MajorVO());
    }

    @Override
    public Result listMajorByGrade(GradeVO gradeVO) {
        List<MajorDO> list = majorMapper.listMajorByGrade(gradeVO);
        return new Result(Result.CODE_SUCCESS, list);
    }

}